package com.yueka.controller;

import com.yueka.pojo.Result;
import com.yueka.pojo.course.CourseRecord;
import com.yueka.service.CourseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RequestMapping("/courseRecord")
@RestController
public class CourseRecordController {

    @Autowired
    private CourseRecordService courseRecordService;
    @GetMapping("/list")
    public Result list(String schoolTime, String status, Integer page, Integer pageSize) {
        try {
            return Result.success(courseRecordService.list(schoolTime, status, page, pageSize));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result deleteById(@RequestBody CourseRecord courseRecord) {
        try {
            courseRecordService.deleteById(courseRecord);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody CourseRecord courseRecord) {
        try {
            courseRecordService.insert(courseRecord);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody CourseRecord courseRecord) {
        try {
            courseRecordService.update(courseRecord);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/byStudentId")
    public Result byStudentId(Integer studentId, LocalDateTime start, LocalDateTime end, String crType, Integer page, Integer pageSize) {
        try {
            return Result.success(courseRecordService.getByStudentId(studentId, start, end, crType, page, pageSize));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/byTeacherId")
    public Result byTeacherId(Integer teacherId, LocalDateTime start, LocalDateTime end, Integer page, Integer pageSize) {
        try {
            return Result.success(courseRecordService.getByTeacherId(teacherId, start, end, page, pageSize));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/openCourse")
    public Result openCourse(LocalDateTime date) {
        try {
            return Result.success(courseRecordService.openCourse(date));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/subscribe")
    public Result subscribe(Integer studentId, Integer courseId) {
        try {
            courseRecordService.subscribe(studentId, courseId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
